﻿using System;
using System.Diagnostics;

namespace Simp.Model.CoreExpressions
{
	/// <summary>Encapsulates an integer as an <see cref="IExpression"/>.</summary>
	/// <remarks>This is a primitive expression, as it doesn't have sub expressions and can't therefore be simplified.</remarks>
	[ExpressionName(IntegerExpressionName)]
	[ParameterPolicy(0)]
	[DebuggerDisplay("{ExpressionHelper.Serialize(this)}")]
	public class IntegerExpression : IExpression, INumericExpression, IExpressionSerializer
	{
		private readonly int _value;

		/// <summary>Name of the expression.</summary>
		public const string IntegerExpressionName = "Integer";

		/// <summary>Constructor accepting the integer value.</summary>
		/// <param name="value"></param>
		public IntegerExpression(int value)
		{
			_value = value;
		}

		/// <summary>Integer value encapsulated in this expression.</summary>
		public int Value
		{
			get { return _value; }
		}

		#region INumericExpression Members
		IntegerExpression INumericExpression.GetInteger()
		{
			return this;
		}

		RationalExpression INumericExpression.GetRational()
		{
			return null;
		}

		RealExpression INumericExpression.GetReal(bool force)
		{
			return force ? new RealExpression(_value) : null;
		}
		#endregion

		#region IExpressionSerializer Members
		void IExpressionSerializer.Serialize(AppendOnlyStringBuilder builder, params IExpression[] parameters)
		{
			if (builder == null)
			{
				throw new ArgumentNullException("builder");
			}
			if (parameters == null)
			{
				throw new ArgumentNullException("parameters");
			}

			builder.Append(Value.ToString());
		}
		#endregion
	}
}